#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
//TODO : remove unnecessary imports

#include "handLocaliser.h"

//TODO : Decide/test which method is better
// HANDLOC_PIXEL_MODE or HANDLOC_PATCH_MODE
#define HANDLOC_PIXEL_MODE


CvHistogram* HandLocaliser::computeHistogram( IplImage** hsPlanes ) {
	
	//TODO : Correctness check? Maybe assertions?

	CvHistogram* hist;
	// int h_bins = 30, s_bins = 32; 
	int h_bins = 16, s_bins = 16;
	int    hist_size[] = { h_bins, s_bins };
	float  h_ranges[]  = { 0, 180 };          // hue is [0,180]
	float  s_ranges[]  = { 0, 255 }; 
	float* ranges[]    = { h_ranges, s_ranges };
	//int scale = 10; //TODO: Use?
	
	// Build the histogram and compute its contents.
	//
	hist = cvCreateHist( 
						   2, 
						   hist_size, 
						   CV_HIST_ARRAY, 
						   ranges, 
						   1 
						   ); 
	cvCalcHist( hsPlanes, hist, 0, 0 );
	//FIX: Is this left unchanged? I think so, despite it not accepting a const img input
	
#ifdef HANDLOC_PATCH_MODE
	cvNormalizeHist( hist, 1.0 ); //Don't normalize for cvCalcBackProject(), 
#endif
	
	return hist;
}


IplImage* HandLocaliser::createHSVImage( const IplImage* src ) {
	
	IplImage* hsvImage = cvCreateImage( cvGetSize(src), 8, 3 ); 
	cvCvtColor( src, hsvImage, CV_BGR2HSV );
	
	return hsvImage;
	
}
	
//	//TODO: Do we have to assign v_plane?
//	//Documentation seems to suggest it is necessary to have all.
//	h_plane  = cvCreateImage( cvGetSize(src), 8, 1 );
//	s_plane  = cvCreateImage( cvGetSize(src), 8, 1 );
//	v_plane  = cvCreateImage( cvGetSize(src), 8, 1 );
//	planes[0] = h_plane;
//	planes[1] = s_plane;
//	cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 );



//--- display code ---
//
//
//IplImage* src[2],*dst=0,*ftmp=0; //dst is what to display on
//
//
//cvZero(dst);
//
//#define patchx 61
//#define patchy 61
//		
//if(patch){ //patch methods work on img - minus boundary corresponding 1/2 * patch
//	int iwidth = src[1]->width - patchx + 1;
//	int iheight = src[1]->height - patchy + 1;
//	ftmp = cvCreateImage( cvSize(iwidth,iheight),32,1);
//	cvZero(ftmp);
//}
//	
//dst = cvCreateImage( cvGetSize(src[1]),8,1);

